clear all
cd "C:\Users\Public\Documents\ImmPanelRevis19\"
cd "ImmPanelRevis19\Donnees\select"
/* Estimates using current location */
/********************/
/* Wages unbalanced */
/********************/
capture program drop wagescs
program define wagescs
version 11
syntax, occp(name)

use "C:\Users\Public\Documents\ImmPanelRevis19\ImmPanelRevis19\Donnees\select\unbalanced.dta", clear
keep if `occp'==1

/* location in the decile of the initial group distribution */
gen lsn=ln(sn)
/* residual wages for each year */
quietly: reg lsn F_* if ptreat | dtreat
predict rlsn, residuals

save temp1.dta, replace
collapse (mean) prlsn=rlsn if ptreat, by(ze an)
save p1.dta, replace
use temp1.dta, clear
collapse (mean) drlsn=rlsn if dtreat, by(ze an)
save p2.dta, replace
use temp1.dta, clear
collapse dimm dimm75 Y_*, by(ze an)
joinby ze an using p1.dta, unm(m)
drop _merge
joinby ze an using p2.dta, unm(m)
drop _merge

egen ize=group(ze)
egen time=group(an)
xtset ize time
gen rlsn=drlsn-L.prlsn
/* OLS */
ivreg2 rlsn dimm Y_*  , partial(Y_* ) cl(ze)
estimates store `occp'ols

/* IV */
ivreg2 rlsn (dimm  = dimm75 ) Y_* , partial(Y_* ) cl(ze)
estimates store `occp'iv
end

wagescs, occp(all) 
wagescs, occp(cad) 
wagescs, occp(tecemp2) 
wagescs, occp(bc) 
wagescs, occp(bcnq) 

capture rm "C:\Users\Public\Documents\Tab21\Table9_AnnualW.rtf"
/* OLS */
esttab allols  cadols tecemp2ols bcols bcnqols ///
using "C:\Users\Public\Documents\Tab21\Table9_AnnualW.rtf" /// 
, append title("OLS_CS")  b(%9.3f) cells(b(star fmt(3)) se(fmt(3) par) ) ///
 stats(N rkf) star(* 0.10 ** 0.05 *** 0.01)

/* IV */
esttab alliv  cadiv tecemp2iv bciv bcnqiv ///
using "C:\Users\Public\Documents\Tab21\Table9_AnnualW.rtf" /// 
, append title("2SLS-CS")  b(%9.3f) cells(b(star fmt(3)) se(fmt(3) par) ) ///
 stats(N rkf) star(* 0.10 ** 0.05 *** 0.01)

/*********************/
/* WAGES WAGES WAGES */
/*********************/
capture program drop wages
program define wages
version 11
syntax, occp(name) 

/********************/
/* Balanced Sample  */
/********************/
use "C:\Users\Public\Documents\ImmPanelRevis19\ImmPanelRevis19\Donnees\select\final.dta", clear

/* keep individuals in the occupation group */
keep if (btime==1 & `occp') | (btime==2 & L.`occp')

/* location in the decile of the initial group distribution */
gen lsn=ln(sn)
/* residual wages for each year */
quietly: reg lsn F_* 
predict rlsn, residuals
/* change in residual wages */
gen drlsn=D.rlsn

gen ldp=ln(dp/l.dp)
gen oshift=(`occp'==0 & L.`occp'==1)
keep if btime==2 & L.`occp' & !missing(dimm) & !missing(dimm75)
foreach year in 1982 1991 1999 2007 {
/* winsorize drlsn */
quietly: sum drlsn if an==`year', d
replace drlsn=. if drlsn>`r(p99)' & an==`year'
replace drlsn=. if drlsn<`r(p1)' & an==`year'
}
drop if missing(drlsn)
drop if missing(basman)
drop if missing(bascom)

/* weight inverse of the size of the group in the CZ */
save temp.dta, replace
gen dummy=1
collapse (sum) nbze = dummy , by(ze an)
gen iw2=1/ nbze
drop nbze

save iw2.dta, replace
use temp.dta, replace
capture drop _merge
joinby ze an using iw2.dta , unm(m)
drop _merge

save temp1.dta, replace
gen dummy=1

collapse (sum) nbze = dummy (mean) drlsn dimm dimm75 Y_*, by(ze an)

/* OLS */
ivreg2 drlsn dimm Y_*  , partial(Y_* ) cl(ze)
estimates store bs`occp'ols

/* IV */
ivreg2 drlsn (dimm  = dimm75 ) Y_* , partial(Y_* ) cl(ze)
estimates store bs`occp'iv

gen sqnbze=sqrt(nbze)
/* IV */
ivreg2 drlsn (dimm  = dimm75 ) Y_* [aweight = sqnbze] , partial(Y_* ) cl(ze)
estimates store wbs`occp'iv

end

wages, occp(all) 
wages, occp(cad) 
wages, occp(tecemp2) 
wages, occp(bc) 
wages, occp(bcnq) 
/* Estimates using the baseline location */
/* OLS */
esttab bsallols  bscadols bstecemp2ols bsbcols bsbcnqols ///
using "C:\Users\Public\Documents\Tab21\Table9_AnnualW.rtf" /// 
, append title("bsOLS")  b(%9.3f) cells(b(star fmt(3)) se(fmt(3) par) ) ///
 stats(N widstat) star(* 0.10 ** 0.05 *** 0.01)
/* 2SLS */
esttab bsalliv  bscadiv bstecemp2iv bsbciv bsbcnqiv ///
using "C:\Users\Public\Documents\Tab21\Table9_AnnualW.rtf" /// 
, append title("bsIV")  b(%9.3f) cells(b(star fmt(3)) se(fmt(3) par) ) ///
 stats(N widstat) star(* 0.10 ** 0.05 *** 0.01)

 
estout wbsalliv  wbscadiv wbstecemp2iv wbsbciv wbsbcnqiv , cells(b(star fmt(%9.3f)) ///
se(par(`"="("'`")""'))) stats(N r2 rkf) starlevels(* 0.10 ** 0.05 *** 0.01)
